package com.fs.dao.Impl;

import com.fs.dao.ClassDao;
import com.fs.pojo.Class;
import com.fs.pojo.ClassStudent;
import com.fs.utils.IdWorker;
import com.fs.utils.JDBCUtils;
import com.fs.utils.MyConnectionPool;
import com.fs.vo.CourseClassVO;

import java.sql.JDBCType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImpl implements ClassDao {
    @Override
    public Class queryById(String id) {
        String sql = "select * from tb_class where id = ?";
        List<Class> classes = JDBCUtils.executeQurey(sql, Class.class, id);
        return classes == null ? null : classes.get(0);
    }

    @Override
    public List queryAll() {
        String sql = "select * from tb_class";
        return JDBCUtils.executeQurey(sql,Class.class);
    }

    @Override
    public int insert(Class obj) {
        String sql = "insert into tb_class (id,course_id,class_name, introduce, total, code) values (?,?,?,?,?,?)";
        return JDBCUtils.executeUpdate(sql,Class.class,obj);
    }

    @Override
    public int deleteById(String id) {
        String sql = "delete from tb_class where id = ?";
        return JDBCUtils.executeUpdate(sql,id);
    }

    @Override
    public int update(Class obj) {
        String sql = "update tb_class c set c.class_name=?,c.introduce=?,c.code=? where c.id =?";
        return JDBCUtils.executeUpdate(sql,obj.getClassName(),obj.getIntroduce(),obj.getCode(),obj.getId());
    }

    @Override
    public Class QueryAllByCourseCode(String code) {
        String sql = "select * from tb_class where code = ?";
        List<Class> classes = JDBCUtils.executeQurey(sql, Class.class, code);
        return classes == null ? null : classes.get(0);
    }

    @Override
    public Class QueryAllByCourseId(String courseId) {
        String sql = "select * from tb_class where course_id = ?";
        List<Class> classes = JDBCUtils.executeQurey(sql, Class.class, courseId);
        return classes == null ? null : classes.get(0);
    }

    @Override
    public List<Class> queryClassesByCourseId(String courseId) {
        String sql = "select * from tb_class where course_id = ?";


        return JDBCUtils.executeQurey(sql,Class.class,courseId);
    }

    @Override
    public String insertReturnId(Class cla) {
        cla.setTotal(0L);
        IdWorker idWorker =  new IdWorker();
        String id = String.valueOf(idWorker.nextId());
        cla.setId(id);

        String sql = "INSERT INTO tb_class(id, course_id, class_name, introduce, total, code) values(?,?,?,?,?,?) ";


        return JDBCUtils.executeUpdate(sql,cla.getId(),cla.getCourseId(),cla.getClassName(),cla.getIntroduce(),cla.getTotal(),cla.getCode())>0
                ?id:null;
    }

    @Override
    public Class queryClassByCourseIdAndstudentId(String studentId) {
        String sql = "select * from tb_class where id = (select class_id from tb_class_student where  student_id  = ?) ";
        List<Class> list = JDBCUtils.executeQurey(sql,Class.class,studentId);
        if( list!= null && !list.isEmpty())
        {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<CourseClassVO> qurycourseNameAndClassByTeacherId(String teacherId) {
        String sql = "SELECT class.id classId,class.class_name className,  course.course_name courseName, course.id courseId FROM tb_class class,tb_course course WHERE class.course_id = course.id AND course.teacher_id = ?";
        Connection connection = null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet = null;
        try {
            connection = MyConnectionPool.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,teacherId);
            resultSet = preparedStatement.executeQuery();
            List<CourseClassVO> courseClassVOS = new ArrayList<>();
            while(resultSet.next()){
                boolean flag = false;
                if (courseClassVOS != null) {
                    for (int i = 0; i < courseClassVOS.size(); i++) {
                        if (courseClassVOS.get(i).getId().equals(resultSet.getString("courseId")) ){
                            Class aClass = new Class();
                            aClass.setClassName(resultSet.getString("className"));
                            aClass.setId(resultSet.getString("classId"));
                            courseClassVOS.get(i).getClassList().add(aClass);
                            flag = true;
                            break;
                        }

                    }
                }
                if (flag==true) continue;
                CourseClassVO courseClassVO = new CourseClassVO();
                courseClassVO.setId(resultSet.getString("courseId"));
                courseClassVO.setCourseName(resultSet.getString("courseName"));
                Class aClass = new Class();
                aClass.setClassName(resultSet.getString("className"));
                aClass.setId(resultSet.getString("classId"));
                courseClassVO.getClassList().add(aClass);
                courseClassVOS.add(courseClassVO);
            }
            return courseClassVOS;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
        return null;
    }



}
